package gui.partb;

import java.util.List;

/**
 *
 *
 * @author guodd
 * @version 1.0 use jdk 1.8
 */
public class DataAccess {

    public static void saveToFile(List<Object> objs, String url) {
        FileUtils.writeObjectToFile(objs, url);
    }

    public static void getStudentById(String stuId, String url) {
        // 学生id
        List<?> studentAll = (List<?>) FileUtils.readObjectFromFile(url);
        if (studentAll != null && studentAll.size() > 0) {
            boolean flag = true;
            for (Object o : studentAll) {
                Student s = (Student) o;
                if (s.getStuId().equals(stuId)) {
                    System.out.println("the student information is:" + s);
                    flag = false;
                }
            }
            if (flag) {
                System.out.println("no student found!");
            }
        }
    }

    public static void getCourseById(String courseId, String url) {
        List<?> courseAll = (List<?>) FileUtils.readObjectFromFile(url);
        if (courseAll != null && courseAll.size() > 0) {
            boolean flag = true;
            for (Object o : courseAll) {
                Course s = (Course) o;
                if (s.getCourseId().equals(courseId)) {
                    System.out.println("the course information is:" + s);
                    flag = false;
                }
            }
            if (flag) {
                System.out.println("no course found!");
            }
        }
    }

    public static void getTeacherById(String teacherId, String url) {
        List<?> teacherAll = (List<?>) FileUtils.readObjectFromFile(url);
        if (teacherAll != null && teacherAll.size() > 0) {
            boolean flag = true;
            for (Object o : teacherAll) {
                Teacher s = (Teacher) o;
                if (s.getTeacherId().equals(teacherId)) {
                    System.out.println("the teacher information is:" + s);
                    flag = false;
                }
            }
            if (flag) {
                System.out.println("no teacher found!");
            }
        }
    }

    public static void getGradeById(String grade, String url) {
        List<?> gradeAll = (List<?>) FileUtils.readObjectFromFile(url);
        if (gradeAll != null && gradeAll.size() > 0) {
            boolean flag = true;
            for (Object o : gradeAll) {
                Grade s = (Grade) o;
                if (s.getGrade().equals(grade)) {
                    System.out.println("the grade information is:" + s);
                    flag = false;
                }
            }
            if (flag) {
                System.out.println("no grade found!");
            }
        }
    }
}
